<?php

/**
 * This is the model class for table "gadgetcategory".
 *
 * The followings are the available columns in table 'gadgetcategory':
 * @property integer $id
 * @property string $name
 * @property string $description
 * @property integer $parentCategoryId
 * @property string $imageUrl
 * @property string $templateFile
 * @property string $urlKey
 * @property integer $isActive
 *
 * The followings are the available model relations:
 * @property Gadget[] $gadgets
 */
class Gadgetcategory extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Gadgetcategory the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'gadgetcategory';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name', 'required'),
			array('parentCategoryId, isActive', 'numerical', 'integerOnly'=>true),
			array('name, urlKey', 'length', 'max'=>50),
			array('description', 'length', 'max'=>255),
			array('imageUrl, templateFile', 'length', 'max'=>100),
			//array('imageUrl', 'file', 'types'=>'jpg, gif, png'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, description, parentCategoryId, imageUrl, templateFile, urlKey, isActive', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'gadgets' => array(self::HAS_MANY, 'Gadget', 'categoryId'),
		);
	}
	public function __filterAlias($strSiteAlias) {
        $value = trim($strSiteAlias);
        $value = preg_replace('/[^A-Za-z0-9\s_-]/', '', $value);      // alow only: a-z, A-Z, 0-9, (_), (-)
        $value = preg_replace('/\s/', '-', $value);                   // replace whitespace to (-)
        $value = strtolower($value);                                  // convert to Lower Case
        return $value;
    }
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'description' => 'Description',
			'parentCategoryId' => 'Parent Category',
			'imageUrl' => 'Image Url',
			'templateFile' => 'Template File',
			'urlKey' => 'Url Key',
			'isActive' => 'Is Active',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		
		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('parentCategoryId',$this->parentCategoryId);
		$criteria->compare('imageUrl',$this->imageUrl,true);
		$criteria->compare('templateFile',$this->templateFile,true);
		$criteria->compare('urlKey',$this->urlKey,true);
		$criteria->compare('isActive',$this->isActive);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function getCategory($id=null){
		if($id!=null){
			
			$model = $this->findByPk($id);
			
			if(!empty($model))
				return $model->name;
			else
				return null;

		}
		if($this->parentCategoryId!=null){
			$model = $this->findByPk($this->parentCategoryId);
			if(!empty($model))
				return $model->name;
			else
				return null;
		}else 	return null;
	}
	public function listDataCat(){
		$models = $this->findAll();
		$list = CHtml::listData($models, 
                'id', 'name');  
		return $list;
	}
	public function getAll(){
		
		$models  = $this->findAllByAttributes(array(
			'isActive'=>1,
			'parentCategoryId'=>null,
		),array('order'=>'id asc'));
		
		return $models;
	}

}